
<!DOCTYPE html>

<html>
  
<!-- Mirrored from docs.sympy.org/latest/modules/combinatorics/graycode.html by HTTrack Website Copier/3.x [XR&CO'2014], Sat, 15 Jan 2022 03:26:03 GMT -->
<!-- Added by HTTrack --><meta http-equiv="content-type" content="text/html;charset=utf-8" /><!-- /Added by HTTrack -->
<head>
    <meta charset="utf-8" />
    <meta name="viewport" content="width=device-width, initial-scale=1.0" /><meta name="generator" content="Docutils 0.17.1: http://docutils.sourceforge.net/" />

    <title>Gray Code &#8212; SymPy 1.9 documentation</title>
    <link rel="stylesheet" type="text/css" href="../../_static/pygments.css" />
    <link rel="stylesheet" type="text/css" href="../../_static/default.css" />
    <link rel="stylesheet" type="text/css" href="../../_static/graphviz.css" />
    <link rel="stylesheet" type="text/css" href="../../_static/plot_directive.css" />
    <link rel="stylesheet" type="text/css" href="../../../../live.sympy.org/static/live-core.css" />
    <link rel="stylesheet" type="text/css" href="../../../../live.sympy.org/static/live-autocomplete.css" />
    <link rel="stylesheet" type="text/css" href="../../../../live.sympy.org/static/live-sphinx.css" />
    
    <script data-url_root="../../" id="documentation_options" src="../../_static/documentation_options.js"></script>
    <script src="../../_static/jquery.js"></script>
    <script src="../../_static/underscore.js"></script>
    <script src="../../_static/doctools.js"></script>
    <script src="../../../../live.sympy.org/static/utilities.js"></script>
    <script src="../../../../live.sympy.org/static/external/classy.js"></script>
    <script src="../../../../live.sympy.org/static/live-core.js"></script>
    <script src="../../../../live.sympy.org/static/live-autocomplete.js"></script>
    <script src="../../../../live.sympy.org/static/live-sphinx.js"></script>
    
    <link rel="shortcut icon" href="../../_static/sympy-notailtext-favicon.ico"/>
    <link href="graycode.html" rel="canonical" />
    
    <link rel="index" title="Index" href="../../genindex.html" />
    <link rel="search" title="Search" href="../../search.html" />
    <link rel="next" title="Named Groups" href="named_groups.html" />
    <link rel="prev" title="Subsets" href="subsets.html" /> 
  </head><body>
    <div class="related" role="navigation" aria-label="related navigation">
      <h3>Navigation</h3>
      <ul>
        <li class="right" style="margin-right: 10px">
          <a href="../../genindex.html" title="General Index"
             accesskey="I">index</a></li>
        <li class="right" >
          <a href="../../py-modindex.html" title="Python Module Index"
             >modules</a> |</li>
        <li class="right" >
          <a href="named_groups.html" title="Named Groups"
             accesskey="N">next</a> |</li>
        <li class="right" >
          <a href="subsets.html" title="Subsets"
             accesskey="P">previous</a> |</li>
        <li class="nav-item nav-item-0"><a href="../../index.html">SymPy 1.9 documentation</a> &#187;</li>
          <li class="nav-item nav-item-1"><a href="../index.html" >SymPy Modules Reference</a> &#187;</li>
          <li class="nav-item nav-item-2"><a href="index.html" accesskey="U">Combinatorics</a> &#187;</li>
        <li class="nav-item nav-item-this"><a href="#">Gray Code</a></li> 
      </ul>
    </div>  

    <div class="document">
      <div class="documentwrapper">
        <div class="bodywrapper">
          <div class="body" role="main">
            
  <section id="module-sympy.combinatorics.graycode">
<span id="gray-code"></span><span id="combinatorics-graycode"></span><h1>Gray Code<a class="headerlink" href="#module-sympy.combinatorics.graycode" title="Permalink to this headline">¶</a></h1>
<dl class="py class">
<dt class="sig sig-object py" id="sympy.combinatorics.graycode.GrayCode">
<em class="property"><span class="pre">class</span> </em><span class="sig-prename descclassname"><span class="pre">sympy.combinatorics.graycode.</span></span><span class="sig-name descname"><span class="pre">GrayCode</span></span><span class="sig-paren">(</span><em class="sig-param"><span class="n"><span class="pre">n</span></span></em>, <em class="sig-param"><span class="o"><span class="pre">*</span></span><span class="n"><span class="pre">args</span></span></em>, <em class="sig-param"><span class="o"><span class="pre">**</span></span><span class="n"><span class="pre">kw_args</span></span></em><span class="sig-paren">)</span><a class="reference external" href="https://github.com/sympy/sympy/blob/00d6469eafdd4aac346a0b598184c15f2560dbe5/sympy/combinatorics/graycode.py#L6-L319"><span class="viewcode-link"><span class="pre">[source]</span></span></a><a class="headerlink" href="#sympy.combinatorics.graycode.GrayCode" title="Permalink to this definition">¶</a></dt>
<dd><p>A Gray code is essentially a Hamiltonian walk on
a n-dimensional cube with edge length of one.
The vertices of the cube are represented by vectors
whose values are binary. The Hamilton walk visits
each vertex exactly once. The Gray code for a 3d
cube is [‘000’,’100’,’110’,’010’,’011’,’111’,’101’,
‘001’].</p>
<p>A Gray code solves the problem of sequentially
generating all possible subsets of n objects in such
a way that each subset is obtained from the previous
one by either deleting or adding a single object.
In the above example, 1 indicates that the object is
present, and 0 indicates that its absent.</p>
<p>Gray codes have applications in statistics as well when
we want to compute various statistics related to subsets
in an efficient manner.</p>
<p class="rubric">Examples</p>
<div class="doctest highlight-default notranslate"><div class="highlight"><pre><span></span><span class="gp">&gt;&gt;&gt; </span><span class="kn">from</span> <span class="nn">sympy.combinatorics.graycode</span> <span class="kn">import</span> <span class="n">GrayCode</span>
<span class="gp">&gt;&gt;&gt; </span><span class="n">a</span> <span class="o">=</span> <span class="n">GrayCode</span><span class="p">(</span><span class="mi">3</span><span class="p">)</span>
<span class="gp">&gt;&gt;&gt; </span><span class="nb">list</span><span class="p">(</span><span class="n">a</span><span class="o">.</span><span class="n">generate_gray</span><span class="p">())</span>
<span class="go">[&#39;000&#39;, &#39;001&#39;, &#39;011&#39;, &#39;010&#39;, &#39;110&#39;, &#39;111&#39;, &#39;101&#39;, &#39;100&#39;]</span>
<span class="gp">&gt;&gt;&gt; </span><span class="n">a</span> <span class="o">=</span> <span class="n">GrayCode</span><span class="p">(</span><span class="mi">4</span><span class="p">)</span>
<span class="gp">&gt;&gt;&gt; </span><span class="nb">list</span><span class="p">(</span><span class="n">a</span><span class="o">.</span><span class="n">generate_gray</span><span class="p">())</span>
<span class="go">[&#39;0000&#39;, &#39;0001&#39;, &#39;0011&#39;, &#39;0010&#39;, &#39;0110&#39;, &#39;0111&#39;, &#39;0101&#39;, &#39;0100&#39;,     &#39;1100&#39;, &#39;1101&#39;, &#39;1111&#39;, &#39;1110&#39;, &#39;1010&#39;, &#39;1011&#39;, &#39;1001&#39;, &#39;1000&#39;]</span>
</pre></div>
</div>
<p class="rubric">References</p>
<dl class="citation">
<dt class="label" id="r38"><span class="brackets"><a class="fn-backref" href="#id1">R38</a></span></dt>
<dd><p>Nijenhuis,A. and Wilf,H.S.(1978).
Combinatorial Algorithms. Academic Press.</p>
</dd>
<dt class="label" id="r39"><span class="brackets"><a class="fn-backref" href="#id2">R39</a></span></dt>
<dd><p>Knuth, D. (2011). The Art of Computer Programming, Vol 4
Addison Wesley</p>
</dd>
</dl>
<dl class="py property">
<dt class="sig sig-object py" id="sympy.combinatorics.graycode.GrayCode.current">
<em class="property"><span class="pre">property</span> </em><span class="sig-name descname"><span class="pre">current</span></span><a class="headerlink" href="#sympy.combinatorics.graycode.GrayCode.current" title="Permalink to this definition">¶</a></dt>
<dd><p>Returns the currently referenced Gray code as a bit string.</p>
<p class="rubric">Examples</p>
<div class="doctest highlight-default notranslate"><div class="highlight"><pre><span></span><span class="gp">&gt;&gt;&gt; </span><span class="kn">from</span> <span class="nn">sympy.combinatorics.graycode</span> <span class="kn">import</span> <span class="n">GrayCode</span>
<span class="gp">&gt;&gt;&gt; </span><span class="n">GrayCode</span><span class="p">(</span><span class="mi">3</span><span class="p">,</span> <span class="n">start</span><span class="o">=</span><span class="s1">&#39;100&#39;</span><span class="p">)</span><span class="o">.</span><span class="n">current</span>
<span class="go">&#39;100&#39;</span>
</pre></div>
</div>
</dd></dl>

<dl class="py method">
<dt class="sig sig-object py" id="sympy.combinatorics.graycode.GrayCode.generate_gray">
<span class="sig-name descname"><span class="pre">generate_gray</span></span><span class="sig-paren">(</span><em class="sig-param"><span class="o"><span class="pre">**</span></span><span class="n"><span class="pre">hints</span></span></em><span class="sig-paren">)</span><a class="reference external" href="https://github.com/sympy/sympy/blob/00d6469eafdd4aac346a0b598184c15f2560dbe5/sympy/combinatorics/graycode.py#L150-L201"><span class="viewcode-link"><span class="pre">[source]</span></span></a><a class="headerlink" href="#sympy.combinatorics.graycode.GrayCode.generate_gray" title="Permalink to this definition">¶</a></dt>
<dd><p>Generates the sequence of bit vectors of a Gray Code.</p>
<p class="rubric">Examples</p>
<div class="doctest highlight-default notranslate"><div class="highlight"><pre><span></span><span class="gp">&gt;&gt;&gt; </span><span class="kn">from</span> <span class="nn">sympy.combinatorics.graycode</span> <span class="kn">import</span> <span class="n">GrayCode</span>
<span class="gp">&gt;&gt;&gt; </span><span class="n">a</span> <span class="o">=</span> <span class="n">GrayCode</span><span class="p">(</span><span class="mi">3</span><span class="p">)</span>
<span class="gp">&gt;&gt;&gt; </span><span class="nb">list</span><span class="p">(</span><span class="n">a</span><span class="o">.</span><span class="n">generate_gray</span><span class="p">())</span>
<span class="go">[&#39;000&#39;, &#39;001&#39;, &#39;011&#39;, &#39;010&#39;, &#39;110&#39;, &#39;111&#39;, &#39;101&#39;, &#39;100&#39;]</span>
<span class="gp">&gt;&gt;&gt; </span><span class="nb">list</span><span class="p">(</span><span class="n">a</span><span class="o">.</span><span class="n">generate_gray</span><span class="p">(</span><span class="n">start</span><span class="o">=</span><span class="s1">&#39;011&#39;</span><span class="p">))</span>
<span class="go">[&#39;011&#39;, &#39;010&#39;, &#39;110&#39;, &#39;111&#39;, &#39;101&#39;, &#39;100&#39;]</span>
<span class="gp">&gt;&gt;&gt; </span><span class="nb">list</span><span class="p">(</span><span class="n">a</span><span class="o">.</span><span class="n">generate_gray</span><span class="p">(</span><span class="n">rank</span><span class="o">=</span><span class="mi">4</span><span class="p">))</span>
<span class="go">[&#39;110&#39;, &#39;111&#39;, &#39;101&#39;, &#39;100&#39;]</span>
</pre></div>
</div>
<div class="admonition seealso">
<p class="admonition-title">See also</p>
<p><a class="reference internal" href="#sympy.combinatorics.graycode.GrayCode.skip" title="sympy.combinatorics.graycode.GrayCode.skip"><code class="xref py py-obj docutils literal notranslate"><span class="pre">skip</span></code></a></p>
</div>
<p class="rubric">References</p>
<dl class="citation">
<dt class="label" id="r40"><span class="brackets"><a class="fn-backref" href="#id3">R40</a></span></dt>
<dd><p>Knuth, D. (2011). The Art of Computer Programming,
Vol 4, Addison Wesley</p>
</dd>
</dl>
</dd></dl>

<dl class="py property">
<dt class="sig sig-object py" id="sympy.combinatorics.graycode.GrayCode.n">
<em class="property"><span class="pre">property</span> </em><span class="sig-name descname"><span class="pre">n</span></span><a class="headerlink" href="#sympy.combinatorics.graycode.GrayCode.n" title="Permalink to this definition">¶</a></dt>
<dd><p>Returns the dimension of the Gray code.</p>
<p class="rubric">Examples</p>
<div class="doctest highlight-default notranslate"><div class="highlight"><pre><span></span><span class="gp">&gt;&gt;&gt; </span><span class="kn">from</span> <span class="nn">sympy.combinatorics.graycode</span> <span class="kn">import</span> <span class="n">GrayCode</span>
<span class="gp">&gt;&gt;&gt; </span><span class="n">a</span> <span class="o">=</span> <span class="n">GrayCode</span><span class="p">(</span><span class="mi">5</span><span class="p">)</span>
<span class="gp">&gt;&gt;&gt; </span><span class="n">a</span><span class="o">.</span><span class="n">n</span>
<span class="go">5</span>
</pre></div>
</div>
</dd></dl>

<dl class="py method">
<dt class="sig sig-object py" id="sympy.combinatorics.graycode.GrayCode.next">
<span class="sig-name descname"><span class="pre">next</span></span><span class="sig-paren">(</span><em class="sig-param"><span class="n"><span class="pre">delta</span></span><span class="o"><span class="pre">=</span></span><span class="default_value"><span class="pre">1</span></span></em><span class="sig-paren">)</span><a class="reference external" href="https://github.com/sympy/sympy/blob/00d6469eafdd4aac346a0b598184c15f2560dbe5/sympy/combinatorics/graycode.py#L102-L118"><span class="viewcode-link"><span class="pre">[source]</span></span></a><a class="headerlink" href="#sympy.combinatorics.graycode.GrayCode.next" title="Permalink to this definition">¶</a></dt>
<dd><p>Returns the Gray code a distance <code class="docutils literal notranslate"><span class="pre">delta</span></code> (default = 1) from the
current value in canonical order.</p>
<p class="rubric">Examples</p>
<div class="doctest highlight-default notranslate"><div class="highlight"><pre><span></span><span class="gp">&gt;&gt;&gt; </span><span class="kn">from</span> <span class="nn">sympy.combinatorics.graycode</span> <span class="kn">import</span> <span class="n">GrayCode</span>
<span class="gp">&gt;&gt;&gt; </span><span class="n">a</span> <span class="o">=</span> <span class="n">GrayCode</span><span class="p">(</span><span class="mi">3</span><span class="p">,</span> <span class="n">start</span><span class="o">=</span><span class="s1">&#39;110&#39;</span><span class="p">)</span>
<span class="gp">&gt;&gt;&gt; </span><span class="n">a</span><span class="o">.</span><span class="n">next</span><span class="p">()</span><span class="o">.</span><span class="n">current</span>
<span class="go">&#39;111&#39;</span>
<span class="gp">&gt;&gt;&gt; </span><span class="n">a</span><span class="o">.</span><span class="n">next</span><span class="p">(</span><span class="o">-</span><span class="mi">1</span><span class="p">)</span><span class="o">.</span><span class="n">current</span>
<span class="go">&#39;010&#39;</span>
</pre></div>
</div>
</dd></dl>

<dl class="py property">
<dt class="sig sig-object py" id="sympy.combinatorics.graycode.GrayCode.rank">
<em class="property"><span class="pre">property</span> </em><span class="sig-name descname"><span class="pre">rank</span></span><a class="headerlink" href="#sympy.combinatorics.graycode.GrayCode.rank" title="Permalink to this definition">¶</a></dt>
<dd><p>Ranks the Gray code.</p>
<p>A ranking algorithm determines the position (or rank)
of a combinatorial object among all the objects w.r.t.
a given order. For example, the 4 bit binary reflected
Gray code (BRGC) ‘0101’ has a rank of 6 as it appears in
the 6th position in the canonical ordering of the family
of 4 bit Gray codes.</p>
<p class="rubric">Examples</p>
<div class="doctest highlight-default notranslate"><div class="highlight"><pre><span></span><span class="gp">&gt;&gt;&gt; </span><span class="kn">from</span> <span class="nn">sympy.combinatorics.graycode</span> <span class="kn">import</span> <span class="n">GrayCode</span>
<span class="gp">&gt;&gt;&gt; </span><span class="n">a</span> <span class="o">=</span> <span class="n">GrayCode</span><span class="p">(</span><span class="mi">3</span><span class="p">)</span>
<span class="gp">&gt;&gt;&gt; </span><span class="nb">list</span><span class="p">(</span><span class="n">a</span><span class="o">.</span><span class="n">generate_gray</span><span class="p">())</span>
<span class="go">[&#39;000&#39;, &#39;001&#39;, &#39;011&#39;, &#39;010&#39;, &#39;110&#39;, &#39;111&#39;, &#39;101&#39;, &#39;100&#39;]</span>
<span class="gp">&gt;&gt;&gt; </span><span class="n">GrayCode</span><span class="p">(</span><span class="mi">3</span><span class="p">,</span> <span class="n">start</span><span class="o">=</span><span class="s1">&#39;100&#39;</span><span class="p">)</span><span class="o">.</span><span class="n">rank</span>
<span class="go">7</span>
<span class="gp">&gt;&gt;&gt; </span><span class="n">GrayCode</span><span class="p">(</span><span class="mi">3</span><span class="p">,</span> <span class="n">rank</span><span class="o">=</span><span class="mi">7</span><span class="p">)</span><span class="o">.</span><span class="n">current</span>
<span class="go">&#39;100&#39;</span>
</pre></div>
</div>
<div class="admonition seealso">
<p class="admonition-title">See also</p>
<p><a class="reference internal" href="#sympy.combinatorics.graycode.GrayCode.unrank" title="sympy.combinatorics.graycode.GrayCode.unrank"><code class="xref py py-obj docutils literal notranslate"><span class="pre">unrank</span></code></a></p>
</div>
<p class="rubric">References</p>
<dl class="citation">
<dt class="label" id="r41"><span class="brackets"><a class="fn-backref" href="#id4">R41</a></span></dt>
<dd><p><a class="reference external" href="http://statweb.stanford.edu/~susan/courses/s208/node12.html">http://statweb.stanford.edu/~susan/courses/s208/node12.html</a></p>
</dd>
</dl>
</dd></dl>

<dl class="py property">
<dt class="sig sig-object py" id="sympy.combinatorics.graycode.GrayCode.selections">
<em class="property"><span class="pre">property</span> </em><span class="sig-name descname"><span class="pre">selections</span></span><a class="headerlink" href="#sympy.combinatorics.graycode.GrayCode.selections" title="Permalink to this definition">¶</a></dt>
<dd><p>Returns the number of bit vectors in the Gray code.</p>
<p class="rubric">Examples</p>
<div class="doctest highlight-default notranslate"><div class="highlight"><pre><span></span><span class="gp">&gt;&gt;&gt; </span><span class="kn">from</span> <span class="nn">sympy.combinatorics.graycode</span> <span class="kn">import</span> <span class="n">GrayCode</span>
<span class="gp">&gt;&gt;&gt; </span><span class="n">a</span> <span class="o">=</span> <span class="n">GrayCode</span><span class="p">(</span><span class="mi">3</span><span class="p">)</span>
<span class="gp">&gt;&gt;&gt; </span><span class="n">a</span><span class="o">.</span><span class="n">selections</span>
<span class="go">8</span>
</pre></div>
</div>
</dd></dl>

<dl class="py method">
<dt class="sig sig-object py" id="sympy.combinatorics.graycode.GrayCode.skip">
<span class="sig-name descname"><span class="pre">skip</span></span><span class="sig-paren">(</span><span class="sig-paren">)</span><a class="reference external" href="https://github.com/sympy/sympy/blob/00d6469eafdd4aac346a0b598184c15f2560dbe5/sympy/combinatorics/graycode.py#L203-L230"><span class="viewcode-link"><span class="pre">[source]</span></span></a><a class="headerlink" href="#sympy.combinatorics.graycode.GrayCode.skip" title="Permalink to this definition">¶</a></dt>
<dd><p>Skips the bit generation.</p>
<p class="rubric">Examples</p>
<div class="doctest highlight-default notranslate"><div class="highlight"><pre><span></span><span class="gp">&gt;&gt;&gt; </span><span class="kn">from</span> <span class="nn">sympy.combinatorics.graycode</span> <span class="kn">import</span> <span class="n">GrayCode</span>
<span class="gp">&gt;&gt;&gt; </span><span class="n">a</span> <span class="o">=</span> <span class="n">GrayCode</span><span class="p">(</span><span class="mi">3</span><span class="p">)</span>
<span class="gp">&gt;&gt;&gt; </span><span class="k">for</span> <span class="n">i</span> <span class="ow">in</span> <span class="n">a</span><span class="o">.</span><span class="n">generate_gray</span><span class="p">():</span>
<span class="gp">... </span>    <span class="k">if</span> <span class="n">i</span> <span class="o">==</span> <span class="s1">&#39;010&#39;</span><span class="p">:</span>
<span class="gp">... </span>        <span class="n">a</span><span class="o">.</span><span class="n">skip</span><span class="p">()</span>
<span class="gp">... </span>    <span class="nb">print</span><span class="p">(</span><span class="n">i</span><span class="p">)</span>
<span class="gp">...</span>
<span class="go">000</span>
<span class="go">001</span>
<span class="go">011</span>
<span class="go">010</span>
<span class="go">111</span>
<span class="go">101</span>
<span class="go">100</span>
</pre></div>
</div>
<div class="admonition seealso">
<p class="admonition-title">See also</p>
<p><a class="reference internal" href="#sympy.combinatorics.graycode.GrayCode.generate_gray" title="sympy.combinatorics.graycode.GrayCode.generate_gray"><code class="xref py py-obj docutils literal notranslate"><span class="pre">generate_gray</span></code></a></p>
</div>
</dd></dl>

<dl class="py method">
<dt class="sig sig-object py" id="sympy.combinatorics.graycode.GrayCode.unrank">
<em class="property"><span class="pre">classmethod</span> </em><span class="sig-name descname"><span class="pre">unrank</span></span><span class="sig-paren">(</span><em class="sig-param"><span class="n"><span class="pre">n</span></span></em>, <em class="sig-param"><span class="n"><span class="pre">rank</span></span></em><span class="sig-paren">)</span><a class="reference external" href="https://github.com/sympy/sympy/blob/00d6469eafdd4aac346a0b598184c15f2560dbe5/sympy/combinatorics/graycode.py#L288-L319"><span class="viewcode-link"><span class="pre">[source]</span></span></a><a class="headerlink" href="#sympy.combinatorics.graycode.GrayCode.unrank" title="Permalink to this definition">¶</a></dt>
<dd><p>Unranks an n-bit sized Gray code of rank k. This method exists
so that a derivative GrayCode class can define its own code of
a given rank.</p>
<p>The string here is generated in reverse order to allow for tail-call
optimization.</p>
<p class="rubric">Examples</p>
<div class="doctest highlight-default notranslate"><div class="highlight"><pre><span></span><span class="gp">&gt;&gt;&gt; </span><span class="kn">from</span> <span class="nn">sympy.combinatorics.graycode</span> <span class="kn">import</span> <span class="n">GrayCode</span>
<span class="gp">&gt;&gt;&gt; </span><span class="n">GrayCode</span><span class="p">(</span><span class="mi">5</span><span class="p">,</span> <span class="n">rank</span><span class="o">=</span><span class="mi">3</span><span class="p">)</span><span class="o">.</span><span class="n">current</span>
<span class="go">&#39;00010&#39;</span>
<span class="gp">&gt;&gt;&gt; </span><span class="n">GrayCode</span><span class="o">.</span><span class="n">unrank</span><span class="p">(</span><span class="mi">5</span><span class="p">,</span> <span class="mi">3</span><span class="p">)</span>
<span class="go">&#39;00010&#39;</span>
</pre></div>
</div>
<div class="admonition seealso">
<p class="admonition-title">See also</p>
<p><a class="reference internal" href="#sympy.combinatorics.graycode.GrayCode.rank" title="sympy.combinatorics.graycode.GrayCode.rank"><code class="xref py py-obj docutils literal notranslate"><span class="pre">rank</span></code></a></p>
</div>
</dd></dl>

</dd></dl>

<dl class="py method">
<dt class="sig sig-object py" id="sympy.combinatorics.graycode.random_bitstring">
<span class="sig-prename descclassname"><span class="pre">graycode.</span></span><span class="sig-name descname"><span class="pre">random_bitstring</span></span><span class="sig-paren">(</span><span class="sig-paren">)</span><a class="reference external" href="https://github.com/sympy/sympy/blob/00d6469eafdd4aac346a0b598184c15f2560dbe5/sympy/combinatorics/graycode.py#L322-L333"><span class="viewcode-link"><span class="pre">[source]</span></span></a><a class="headerlink" href="#sympy.combinatorics.graycode.random_bitstring" title="Permalink to this definition">¶</a></dt>
<dd><p>Generates a random bitlist of length n.</p>
<p class="rubric">Examples</p>
<div class="doctest highlight-default notranslate"><div class="highlight"><pre><span></span><span class="gp">&gt;&gt;&gt; </span><span class="kn">from</span> <span class="nn">sympy.combinatorics.graycode</span> <span class="kn">import</span> <span class="n">random_bitstring</span>
<span class="gp">&gt;&gt;&gt; </span><span class="n">random_bitstring</span><span class="p">(</span><span class="mi">3</span><span class="p">)</span> 
<span class="go">100</span>
</pre></div>
</div>
</dd></dl>

<dl class="py method">
<dt class="sig sig-object py" id="sympy.combinatorics.graycode.gray_to_bin">
<span class="sig-prename descclassname"><span class="pre">graycode.</span></span><span class="sig-name descname"><span class="pre">gray_to_bin</span></span><span class="sig-paren">(</span><span class="sig-paren">)</span><a class="reference external" href="https://github.com/sympy/sympy/blob/00d6469eafdd4aac346a0b598184c15f2560dbe5/sympy/combinatorics/graycode.py#L336-L357"><span class="viewcode-link"><span class="pre">[source]</span></span></a><a class="headerlink" href="#sympy.combinatorics.graycode.gray_to_bin" title="Permalink to this definition">¶</a></dt>
<dd><p>Convert from Gray coding to binary coding.</p>
<p>We assume big endian encoding.</p>
<p class="rubric">Examples</p>
<div class="doctest highlight-default notranslate"><div class="highlight"><pre><span></span><span class="gp">&gt;&gt;&gt; </span><span class="kn">from</span> <span class="nn">sympy.combinatorics.graycode</span> <span class="kn">import</span> <span class="n">gray_to_bin</span>
<span class="gp">&gt;&gt;&gt; </span><span class="n">gray_to_bin</span><span class="p">(</span><span class="s1">&#39;100&#39;</span><span class="p">)</span>
<span class="go">&#39;111&#39;</span>
</pre></div>
</div>
<div class="admonition seealso">
<p class="admonition-title">See also</p>
<p><a class="reference internal" href="#sympy.combinatorics.graycode.bin_to_gray" title="sympy.combinatorics.graycode.bin_to_gray"><code class="xref py py-obj docutils literal notranslate"><span class="pre">bin_to_gray</span></code></a></p>
</div>
</dd></dl>

<dl class="py method">
<dt class="sig sig-object py" id="sympy.combinatorics.graycode.bin_to_gray">
<span class="sig-prename descclassname"><span class="pre">graycode.</span></span><span class="sig-name descname"><span class="pre">bin_to_gray</span></span><span class="sig-paren">(</span><span class="sig-paren">)</span><a class="reference external" href="https://github.com/sympy/sympy/blob/00d6469eafdd4aac346a0b598184c15f2560dbe5/sympy/combinatorics/graycode.py#L360-L381"><span class="viewcode-link"><span class="pre">[source]</span></span></a><a class="headerlink" href="#sympy.combinatorics.graycode.bin_to_gray" title="Permalink to this definition">¶</a></dt>
<dd><p>Convert from binary coding to gray coding.</p>
<p>We assume big endian encoding.</p>
<p class="rubric">Examples</p>
<div class="doctest highlight-default notranslate"><div class="highlight"><pre><span></span><span class="gp">&gt;&gt;&gt; </span><span class="kn">from</span> <span class="nn">sympy.combinatorics.graycode</span> <span class="kn">import</span> <span class="n">bin_to_gray</span>
<span class="gp">&gt;&gt;&gt; </span><span class="n">bin_to_gray</span><span class="p">(</span><span class="s1">&#39;111&#39;</span><span class="p">)</span>
<span class="go">&#39;100&#39;</span>
</pre></div>
</div>
<div class="admonition seealso">
<p class="admonition-title">See also</p>
<p><a class="reference internal" href="#sympy.combinatorics.graycode.gray_to_bin" title="sympy.combinatorics.graycode.gray_to_bin"><code class="xref py py-obj docutils literal notranslate"><span class="pre">gray_to_bin</span></code></a></p>
</div>
</dd></dl>

<dl class="py method">
<dt class="sig sig-object py" id="sympy.combinatorics.graycode.get_subset_from_bitstring">
<span class="sig-prename descclassname"><span class="pre">graycode.</span></span><span class="sig-name descname"><span class="pre">get_subset_from_bitstring</span></span><span class="sig-paren">(</span><em class="sig-param"><span class="n"><span class="pre">bitstring</span></span></em><span class="sig-paren">)</span><a class="reference external" href="https://github.com/sympy/sympy/blob/00d6469eafdd4aac346a0b598184c15f2560dbe5/sympy/combinatorics/graycode.py#L384-L405"><span class="viewcode-link"><span class="pre">[source]</span></span></a><a class="headerlink" href="#sympy.combinatorics.graycode.get_subset_from_bitstring" title="Permalink to this definition">¶</a></dt>
<dd><p>Gets the subset defined by the bitstring.</p>
<p class="rubric">Examples</p>
<div class="doctest highlight-default notranslate"><div class="highlight"><pre><span></span><span class="gp">&gt;&gt;&gt; </span><span class="kn">from</span> <span class="nn">sympy.combinatorics.graycode</span> <span class="kn">import</span> <span class="n">get_subset_from_bitstring</span>
<span class="gp">&gt;&gt;&gt; </span><span class="n">get_subset_from_bitstring</span><span class="p">([</span><span class="s1">&#39;a&#39;</span><span class="p">,</span> <span class="s1">&#39;b&#39;</span><span class="p">,</span> <span class="s1">&#39;c&#39;</span><span class="p">,</span> <span class="s1">&#39;d&#39;</span><span class="p">],</span> <span class="s1">&#39;0011&#39;</span><span class="p">)</span>
<span class="go">[&#39;c&#39;, &#39;d&#39;]</span>
<span class="gp">&gt;&gt;&gt; </span><span class="n">get_subset_from_bitstring</span><span class="p">([</span><span class="s1">&#39;c&#39;</span><span class="p">,</span> <span class="s1">&#39;a&#39;</span><span class="p">,</span> <span class="s1">&#39;c&#39;</span><span class="p">,</span> <span class="s1">&#39;c&#39;</span><span class="p">],</span> <span class="s1">&#39;1100&#39;</span><span class="p">)</span>
<span class="go">[&#39;c&#39;, &#39;a&#39;]</span>
</pre></div>
</div>
<div class="admonition seealso">
<p class="admonition-title">See also</p>
<p><a class="reference internal" href="#sympy.combinatorics.graycode.graycode_subsets" title="sympy.combinatorics.graycode.graycode_subsets"><code class="xref py py-obj docutils literal notranslate"><span class="pre">graycode_subsets</span></code></a></p>
</div>
</dd></dl>

<dl class="py method">
<dt class="sig sig-object py" id="sympy.combinatorics.graycode.graycode_subsets">
<span class="sig-prename descclassname"><span class="pre">graycode.</span></span><span class="sig-name descname"><span class="pre">graycode_subsets</span></span><span class="sig-paren">(</span><span class="sig-paren">)</span><a class="reference external" href="https://github.com/sympy/sympy/blob/00d6469eafdd4aac346a0b598184c15f2560dbe5/sympy/combinatorics/graycode.py#L408-L430"><span class="viewcode-link"><span class="pre">[source]</span></span></a><a class="headerlink" href="#sympy.combinatorics.graycode.graycode_subsets" title="Permalink to this definition">¶</a></dt>
<dd><p>Generates the subsets as enumerated by a Gray code.</p>
<p class="rubric">Examples</p>
<div class="doctest highlight-default notranslate"><div class="highlight"><pre><span></span><span class="gp">&gt;&gt;&gt; </span><span class="kn">from</span> <span class="nn">sympy.combinatorics.graycode</span> <span class="kn">import</span> <span class="n">graycode_subsets</span>
<span class="gp">&gt;&gt;&gt; </span><span class="nb">list</span><span class="p">(</span><span class="n">graycode_subsets</span><span class="p">([</span><span class="s1">&#39;a&#39;</span><span class="p">,</span> <span class="s1">&#39;b&#39;</span><span class="p">,</span> <span class="s1">&#39;c&#39;</span><span class="p">]))</span>
<span class="go">[[], [&#39;c&#39;], [&#39;b&#39;, &#39;c&#39;], [&#39;b&#39;], [&#39;a&#39;, &#39;b&#39;], [&#39;a&#39;, &#39;b&#39;, &#39;c&#39;],     [&#39;a&#39;, &#39;c&#39;], [&#39;a&#39;]]</span>
<span class="gp">&gt;&gt;&gt; </span><span class="nb">list</span><span class="p">(</span><span class="n">graycode_subsets</span><span class="p">([</span><span class="s1">&#39;a&#39;</span><span class="p">,</span> <span class="s1">&#39;b&#39;</span><span class="p">,</span> <span class="s1">&#39;c&#39;</span><span class="p">,</span> <span class="s1">&#39;c&#39;</span><span class="p">]))</span>
<span class="go">[[], [&#39;c&#39;], [&#39;c&#39;, &#39;c&#39;], [&#39;c&#39;], [&#39;b&#39;, &#39;c&#39;], [&#39;b&#39;, &#39;c&#39;, &#39;c&#39;],     [&#39;b&#39;, &#39;c&#39;], [&#39;b&#39;], [&#39;a&#39;, &#39;b&#39;], [&#39;a&#39;, &#39;b&#39;, &#39;c&#39;], [&#39;a&#39;, &#39;b&#39;, &#39;c&#39;, &#39;c&#39;],     [&#39;a&#39;, &#39;b&#39;, &#39;c&#39;], [&#39;a&#39;, &#39;c&#39;], [&#39;a&#39;, &#39;c&#39;, &#39;c&#39;], [&#39;a&#39;, &#39;c&#39;], [&#39;a&#39;]]</span>
</pre></div>
</div>
<div class="admonition seealso">
<p class="admonition-title">See also</p>
<p><a class="reference internal" href="#sympy.combinatorics.graycode.get_subset_from_bitstring" title="sympy.combinatorics.graycode.get_subset_from_bitstring"><code class="xref py py-obj docutils literal notranslate"><span class="pre">get_subset_from_bitstring</span></code></a></p>
</div>
</dd></dl>

</section>


            <div class="clearer"></div>
          </div>
        </div>
      </div>
      <div class="sphinxsidebar" role="navigation" aria-label="main navigation">
        <div class="sphinxsidebarwrapper">
            <p class="logo"><a href="../../index.html">
              <img class="logo" src="../../_static/sympylogo.png" alt="Logo"/>
            </a></p>
  <h4>Previous topic</h4>
  <p class="topless"><a href="subsets.html"
                        title="previous chapter">Subsets</a></p>
  <h4>Next topic</h4>
  <p class="topless"><a href="named_groups.html"
                        title="next chapter">Named Groups</a></p>
  <div role="note" aria-label="source link">
    <h3>This Page</h3>
    <ul class="this-page-menu">
      <li><a href="../../_sources/modules/combinatorics/graycode.rst.txt"
            rel="nofollow">Show Source</a></li>
    </ul>
   </div>
<div id="searchbox" style="display: none" role="search">
  <h3 id="searchlabel">Quick search</h3>
    <div class="searchformwrapper">
    <form class="search" action="https://docs.sympy.org/latest/search.html" method="get">
      <input type="text" name="q" aria-labelledby="searchlabel" autocomplete="off" autocorrect="off" autocapitalize="off" spellcheck="false"/>
      <input type="submit" value="Go" />
    </form>
    </div>
</div>
<script>$('#searchbox').show(0);</script>
        </div>
      </div>
      <div class="clearer"></div>
    </div>
    <div class="related" role="navigation" aria-label="related navigation">
      <h3>Navigation</h3>
      <ul>
        <li class="right" style="margin-right: 10px">
          <a href="../../genindex.html" title="General Index"
             >index</a></li>
        <li class="right" >
          <a href="../../py-modindex.html" title="Python Module Index"
             >modules</a> |</li>
        <li class="right" >
          <a href="named_groups.html" title="Named Groups"
             >next</a> |</li>
        <li class="right" >
          <a href="subsets.html" title="Subsets"
             >previous</a> |</li>
        <li class="nav-item nav-item-0"><a href="../../index.html">SymPy 1.9 documentation</a> &#187;</li>
          <li class="nav-item nav-item-1"><a href="../index.html" >SymPy Modules Reference</a> &#187;</li>
          <li class="nav-item nav-item-2"><a href="index.html" >Combinatorics</a> &#187;</li>
        <li class="nav-item nav-item-this"><a href="#">Gray Code</a></li> 
      </ul>
    </div>
    <div class="footer" role="contentinfo">
        &#169; Copyright 2021 SymPy Development Team.
      Last updated on Sep 30, 2021.
      Created using <a href="https://www.sphinx-doc.org/">Sphinx</a> 4.1.2.
    </div>
  </body>

<!-- Mirrored from docs.sympy.org/latest/modules/combinatorics/graycode.html by HTTrack Website Copier/3.x [XR&CO'2014], Sat, 15 Jan 2022 03:26:03 GMT -->
</html>